package com.qpcy.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.qpcy.entity.Product;
import com.qpcy.mapper.HotProductMapper;
import com.qpcy.mapper.ProductCaseMapper;
import com.qpcy.mapper.ProductMapper;
import com.qpcy.service.ProductService;
import com.qpcy.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.qpcy.entity.table.ProductTableDef.PRODUCT;
import static com.qpcy.entity.table.ProductCaseTableDef.PRODUCT_CASE;
import static com.qpcy.entity.table.HotProductTableDef.HOT_PRODUCT;

/**
 * 产品 服务层实现。
 *
 * @author LENOVO
 * @since 2024-10-19
 */
@Service
@Transactional
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductCaseMapper productCaseMapper;
    @Autowired
    private HotProductMapper hotProductMapper;

    @Override
    public ResponseResult deleteById(int productId) {
        int delete1 = productMapper.deleteById(productId);
        QueryWrapper queryWrapper = QueryWrapper.create()
                .where(PRODUCT_CASE.PRODUCT_ID.eq(productId));
        QueryWrapper queryWrapper1 = QueryWrapper.create()
                .where(HOT_PRODUCT.PRODUCT_ID.eq(productId));
        int delete2 = productCaseMapper.deleteByQuery(queryWrapper);
        hotProductMapper.deleteByQuery(queryWrapper1);
        return ResponseResult.success();
    }
}
